package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.one.dto.PageResponseDto;
import com.one.dto.SupervisorLoginDTO;
import com.one.dto.SupervisorPageRequestDTO;
import com.one.dto.SupervisorResetDTO;
import com.one.entity.Statistics;
import com.one.entity.Supervisor;
import com.one.exception.BaseException;
import com.one.mapper.SupervisorMapper;
import com.one.service.SupervisorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SupervisorServiceImpl implements SupervisorService {

    @Autowired
    private SupervisorMapper supervisorMapper;

    @Override
    public Supervisor login(SupervisorLoginDTO supervisorLoginDTO) {
        Supervisor supervisor = supervisorMapper.getByTelId(supervisorLoginDTO.getTelId());
        if (supervisor == null) {
            return null; // 监督员不存在
        }
        if (!supervisor.getPassword().equals(supervisorLoginDTO.getPassword())) {
            return null; // 监督员不存在
        }
        return supervisor;
    }

    @Override
    public int resetPassword(SupervisorResetDTO supervisorResetDTO) {
        //查询真实姓名和电话号码，若存在则修改密码，返回1;若不存在则返回0
        QueryWrapper<Supervisor> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("tel_id",supervisorResetDTO.getTelId());
        queryWrapper.eq("real_name",supervisorResetDTO.getRealName());

        if(supervisorMapper.selectOne(queryWrapper)!=null){
            Supervisor supervisor=new Supervisor();
            supervisor.setTelId(supervisorResetDTO.getTelId());
            supervisor.setPassword(supervisorResetDTO.getNewPassword());
            supervisorMapper.updatePasswordByTelId(supervisor);
            return 1;
        }
        return 0;
    }

    @Override
    public PageResponseDto<Supervisor> getSupervisorList(SupervisorPageRequestDTO request) {
        if (request.getPageNum() == null || request.getPageNum() <= 0) {
            request.setPageNum(1);
        }
        PageResponseDto<Supervisor> response = new PageResponseDto<>();

        // 先计算开始查询记录数
        int pageNum = request.getPageNum();
        //TODO 为什么要覆盖，与前端讨论前端传递这个参数意义
        request.setBeginNum((pageNum - 1) * request.getMaxPageNum());

        // 先查询总行数（不使用LIMIT）
        int totalRow = supervisorMapper.countSupervisors(request);
        response.setTotalRow(totalRow);

        // 如果总行数为0，那么直接返回
        if (totalRow == 0) {
            response.setPageNum(pageNum);
            response.setMaxPageNum(request.getMaxPageNum());
            response.setList(new ArrayList<>());
            return response;
        }

        // 计算总页数
        int totalPageNum = (totalRow + request.getMaxPageNum() - 1) / request.getMaxPageNum();
        response.setTotalPageNum(totalPageNum);

        // 计算上一页和下一页
        response.setPreNum(pageNum > 1 ? pageNum - 1 : pageNum);
        response.setNextNum(pageNum < totalPageNum ? pageNum + 1 : pageNum);

        // 查询分页数据
        List<Supervisor> supervisors = supervisorMapper.listSupervisors(request);

        // 给返回值填充余下数据
        response.setPageNum(pageNum);
        response.setMaxPageNum(request.getMaxPageNum());
        response.setBeginNum(request.getBeginNum());
        response.setList(supervisors);

        return response;
    }



    /*
    * 保存监督员信息
    * */
    @Override
    public int saveSupervisor(Supervisor supervisor) {
        // 保存监督员信息
        return supervisorMapper.insert(supervisor);
    }

    /*
    * 根据手机号获取监督员信息，用于检查手机号是否存在，屏蔽密码字段
    * */
    @Override
    public Supervisor getSupervisorById(String telId) {
        Supervisor supervisor = supervisorMapper.getByTelId(telId);
        if (supervisor != null) {
            supervisor.setPassword(null); // 不返回密码
        }
        return supervisor;
    }

    /*
    * 更新监督员信息
    * */
    @Override
    public int updateSupervisor(Supervisor supervisor) {
        return supervisorMapper.updateBytelId(supervisor);
    }

    @Override
    public int deleteBytelId(String telId) {
        return supervisorMapper.delete(new QueryWrapper<Supervisor>().eq("tel_id", telId));
    }
}
